#include "bsp_time.h"
static void MX_PWM_Init(uint16_t period);
static void MX_PUMP_Init(void);

void MX_ATIM_Init(uint16_t period)
{
	 
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_TIMER6);        //定时250us 拥有TDS检测
		rcu_periph_clock_enable(RCU_TIMER5);        //定时1ms 做心跳
	
	
	
	  timer_deinit(TIMER6);

    /* TIMER6 configuration */
    timer_initpara.prescaler         = SystemCoreClock/1000000-1; //1us
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 249;                         //250us
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER6,&timer_initpara);
		
    /* auto-reload preload disable */
//    timer_auto_reload_shadow_disable(TIMER1);
		timer_auto_reload_shadow_enable(TIMER6);
	
		timer_interrupt_flag_clear(TIMER6,TIMER_INT_FLAG_UP);
    /* channel 0 interrupt enable */
    timer_interrupt_enable(TIMER6,TIMER_INT_FLAG_UP);  
			//TIMER0中断优先级设置
	  nvic_irq_enable(TIMER6_IRQn,3,0);//抢占优先级，响应优先级
    /* TIMER6 counter enable */
    timer_enable(TIMER6);
	  
		
    

//	  timer_deinit(TIMER5);

//    /* TIMER6 configuration */
//    timer_initpara.prescaler         = SystemCoreClock-1; //1us
//    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
//    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
//    timer_initpara.period            = 119;                         //1us
//    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
//    timer_initpara.repetitioncounter = 0;
//    timer_init(TIMER5,&timer_initpara);
//		
//    /* auto-reload preload disable */
////    timer_auto_reload_shadow_disable(TIMER1);
//		timer_auto_reload_shadow_enable(TIMER5);
//	
//		timer_interrupt_flag_clear(TIMER5,TIMER_INT_FLAG_UP);
//    /* channel 0 interrupt enable */
//    timer_interrupt_enable(TIMER5,TIMER_INT_FLAG_UP);  
//			//TIMER0中断优先级设置
//	  nvic_irq_enable(TIMER5_IRQn,1,0);//抢占优先级，响应优先级
//    /* TIMER6 counter enable */
//    timer_enable(TIMER5);
		
		
/*******************PWM部分******************/

	  MX_PWM_Init(period);
		MX_PUMP_Init();
		
}
void MX_PUMP_Init(void)
{
	  timer_parameter_struct timer_initpara;
		timer_oc_parameter_struct timer_ocintpara;
	
		rcu_periph_clock_enable(RCU_TIMER0);        //定时250us 拥有TDS检测
		
		gpio_init(HeatWater_PUMP_CTR_GPIO_Port,	GPIO_MODE_AF_PP,	GPIO_OSPEED_50MHZ,	HeatWater_PUMP_CTR_Pin);  //热水泵的pwm
		gpio_init(NorWater_PUMP_CTR_GPIO_Port,	GPIO_MODE_AF_PP,	GPIO_OSPEED_50MHZ,	NorWater_PUMP_CTR_Pin);  //常温水泵的pwm
		
	  timer_deinit(TIMER0);

    /* TIMER6 configuration */
    timer_initpara.prescaler         = SystemCoreClock/1000000*5-1; //5us
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 99;                         //100*5=500us 2k
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER0,&timer_initpara);
		
		
		
		timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
		
		timer_channel_output_config(TIMER0,TIMER_CH_2,&timer_ocintpara);
		timer_channel_output_config(TIMER0,TIMER_CH_3,&timer_ocintpara);
		
			/* TIM3CH2 configuration in PWM mode0,duty cycle 50% */
    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_2,0);         //占空比配置为50%  就是(59+1)/(119+1)
    timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER0,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
		
			/* TIM3CH2 configuration in PWM mode0,duty cycle 50% */
    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_3,0);         //占空比配置为50%  就是(59+1)/(119+1)
    timer_channel_output_mode_config(TIMER0,TIMER_CH_3,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER0,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);
		
		/* TIMER0 primary output enable */
    timer_primary_output_config(TIMER0,ENABLE);
    /* auto-reload preload disable */
		timer_auto_reload_shadow_enable(TIMER0);
	
//		timer_interrupt_flag_clear(TIMER0,TIMER_INT_FLAG_UP);
//    /* channel 0 interrupt enable */
//    timer_interrupt_enable(TIMER0,TIMER_INT_FLAG_UP);  
//			//TIMER0中断优先级设置
//	  nvic_irq_enable(TIMER0_UP_IRQn,1,0);//抢占优先级，响应优先级
    /* TIMER6 counter enable */
    timer_enable(TIMER0);

}
void MX_PWM_Init(uint16_t period)   
{
		timer_oc_parameter_struct timer_ocintpara;
	  rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_AF);

	  rcu_periph_clock_enable(RCU_TIMER3);        //蜂鸣器的pwm
//		rcu_periph_clock_enable(RCU_TIMER0);        //常温水泵和热水泵的pwm

		gpio_init(BUZZER_PWM_GPIO_Port,					GPIO_MODE_AF_PP,	GPIO_OSPEED_50MHZ,	BUZZER_PWM_Pin);  //蜂鸣器的pwm
	
	  timer_deinit(TIMER3);

	  /* TIMER3 configuration */
	  OnBuzzerPWMReset(period);
	  
		/*  TIM0 CH2 configuration in PWM mode   */
		timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
		

    timer_channel_output_config(TIMER3,TIMER_CH_2,&timer_ocintpara);
//		timer_channel_output_config(TIMER0,TIMER_CH_2,&timer_ocintpara);
//		timer_channel_output_config(TIMER0,TIMER_CH_3,&timer_ocintpara);
		
		/* TIM3CH2 configuration in PWM mode0,duty cycle 50% */
    timer_channel_output_pulse_value_config(TIMER3,TIMER_CH_2,59);         //占空比配置为50%  就是(59+1)/(119+1)
    timer_channel_output_mode_config(TIMER3,TIMER_CH_2,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER3,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
		
//		/* TIM0CH2 configuration in PWM mode0,duty cycle 50% */
//    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_2,500);         //占空比配置为0%  热水泵
//    timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_PWM0);
//    timer_channel_output_shadow_config(TIMER0,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
//		/* TIM0CH3 configuration in PWM mode0,duty cycle 50% */
//    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_3,500);         //占空比配置为0%  常温水泵
//    timer_channel_output_mode_config(TIMER0,TIMER_CH_3,TIMER_OC_MODE_PWM0);
//    timer_channel_output_shadow_config(TIMER0,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);
		
		/* auto-reload preload enable */
//    timer_auto_reload_shadow_enable(TIMER0);
		timer_auto_reload_shadow_enable(TIMER3);
    /* auto-reload preload enable */
//    timer_enable(TIMER0);  
    timer_enable(TIMER3);
}




void OnBuzzerPWMReset(uint16_t fre)  //Hz
{
	  timer_parameter_struct timer_initpara;
	   /* TIMER3 configuration  蜂鸣器*/ 
		timer_initpara.prescaler         = 1000000/fre-1; //1us
		timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
		timer_initpara.counterdirection  = TIMER_COUNTER_UP;
		timer_initpara.period            = 119;                         
		timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
		timer_initpara.repetitioncounter = 0;
		timer_init(TIMER3,&timer_initpara);
}
